package com.canbot.u05.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 语音生成界面
 */
public class SpeechGenerationActivity extends Activity {


        private EditText mTTS_et;

        private ToastTools mToastTools;

        private HashMap<String, String> hashMap = new HashMap<>();

        private String eyesID = "4";

        List<String> stringList = new ArrayList<>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_speech_generation);

                mTTS_et = (EditText) findViewById(R.id.speach_generation_et);
                mToastTools = ToastTools.newInstance(this);
                mTTS_et.addTextChangedListener(watcher);
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 2000) {
                                mTTS_et.setText(s.subSequence(0, 2000));
                                mTTS_et.setSelection(2000);
                                ToastTools.newInstance(SpeechGenerationActivity.this).showText(getString(R.string.input_maximum_words));
                        }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }
        };

        /**
         * 返回键
         */
        public void speechback(View view) {
                finish();
        }

        /**
         * 选择表情
         */
        public void iconEmjo(View view) {
                Intent intent = new Intent(SpeechGenerationActivity.this, ChooseActivity.class);
                startActivity(intent);
        }

        /**
         * 保存
         */
        public void holdTTs(View view) {
                String holdTts = mTTS_et.getText().toString();
                if (TextUtils.isEmpty(holdTts)) {
                        ToastTools.newInstance(SpeechGenerationActivity.this).showText(getString(R.string.please_enter_content));
                }
                else {
                        String json2 = SPUtils.getString(SpeechGenerationActivity.this, "Commonlanguage", null);
                        if (json2 != null) {
                                stringList = JSON.parseArray(json2, String.class);
                        }
                        stringList.add(holdTts);
                        String json = JSON.toJSONString(stringList);
                        SPUtils.saveString(SpeechGenerationActivity.this, "Commonlanguage", json);
                        String json1 = SPUtils.getString(SpeechGenerationActivity.this, "Commonlanguage", null);
                        if (json1 != null) {
                                ToastTools.newInstance(SpeechGenerationActivity.this).showText(getString(R.string.save_successfully));
                        }
                        Logger.e("SpeechGenerationActivity", stringList.toString());


                }

        }


        /**
         * 查看常用列表
         */
        public void iconQuick(View view) {
                String json = SPUtils.getString(SpeechGenerationActivity.this, "Commonlanguage", null);

                if (TextUtils.isEmpty(json) || json.length() == 2) {
                        Logger.e("SpeechGenerationActivity", json + "565855555");
                        ToastTools.newInstance(SpeechGenerationActivity.this).showText(getString(R.string.no_common_list));
                }
                else {
                        mTTS_et.setText(null);
                        Intent intent = new Intent(SpeechGenerationActivity.this, HoldTtsActivity.class);
                        intent.putExtra("data", json);
                        startActivityForResult(intent, 120);
                }

        }

        /**
         * 发送
         */
        public void sendTTS(View view) {
                String trim = mTTS_et.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                        mToastTools.showText( getString(R.string.please_enter_content));
                        return;
                }
                hashMap.clear();
                hashMap.put("tts", trim);
                OkHttpUtils.upLoadFile(Url, hashMap, callBack);
        }

        OkHttpUtils.OkHttpCallBack callBack = new OkHttpUtils.OkHttpCallBack() {
                @Override
                public void onSuccess(String result) {
                        mToastTools.showText(result);
                }

                @Override
                public void onFailure() {
                        mToastTools.showText(getString(R.string.network_error));
                }
        };

        private String Url = "http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "actionmusic";

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null) {
                        return;
                }
                if (requestCode == 120 && resultCode == RESULT_OK) {
                        String text=data.getStringExtra("text");
                        mTTS_et.setText(text);
                }
        }
}
